package com.tianji.contract.service.Impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.zxing.WriterException;
import com.tianji.common.Result;
import com.tianji.common.utils.QRCodeGenerator;
import com.tianji.contract.mapper.VisitorAppointmentMapper;
import com.tianji.contract.pojo.VisitorAppointment;
import com.tianji.contract.service.VisitorAppointmentService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.time.LocalDateTime;
import java.util.UUID;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 陈佳
 * @since 2024-10-15
 */
@Service
@RequiredArgsConstructor
public class VisitorAppointmentServiceImpl extends ServiceImpl<VisitorAppointmentMapper, VisitorAppointment> implements VisitorAppointmentService {

    private final VisitorAppointmentMapper visitorAppointmentMapper;

    @Override
    public Result createAppointment(VisitorAppointment appointment) {
        try {
            appointment.setStatus("PENDING");
            appointment.setCreatedAt(LocalDateTime.now());
            // 生成二维码内容
            String qrContent = "姓名: " + appointment.getVisitorName() +
                    "\n电话: " + appointment.getPhone() +
                    "\n放行物品: " + appointment.getItems();
            // 生成二维码图片
            String filePath =  appointment.getPhone() + ".png";
            appointment.setQrCode(filePath);
            visitorAppointmentMapper.insert(appointment);
            QRCodeGenerator.generateQRCode(qrContent, filePath);
            return Result.success(filePath);
        } catch (WriterException | IOException e) {
            e.printStackTrace();
            throw new RuntimeException("二维码生成失败");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
